package com.lyf.language.learn.atsgg.network;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @AUTHOR LYF
 * @DATE 2022/6/4
 * @VERSION 1.0
 * @DESC
 * 案例--
 * 1.客户端发给服务端，服务端打印--见SocketDemo
 * @Link{com.lyf.language.learn.atsgg.network.SocketDemo}
 * 2.客户端发文件给服务端，服务端保存本地，并返回OK给客户端
 */
public class TCPDemo {
    @Test
    public void server(){
        ServerSocket serverSocket = null;
        Socket socket = null;
        //FileOutputStream fileOutputStream = new FileOutputStream("");
        InputStream inputStream = null;
        try {

            serverSocket = new ServerSocket(8899);//InetAddress.getByName("")
            //监听，建立连接
            socket = serverSocket.accept();
            inputStream = socket.getInputStream();
            //FileOutputStream fileOutputStream  = new FileOutputStream(String.valueOf(inputStream));
            //FileUtils.writeStringToFile(new File(""),String.valueOf(inputStream));
            String path = "E:\\JavaProjects\\LearnProjects\\learn-practice\\java-base\\src\\main\\java\\com\\lyf\\language\\learn\\atsgg\\network\\tmp.txt";
            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            // fileOutputStream.write();
            byte[] buffer = new byte[1024];
            int len;
            //read阻塞式---
            while((len=inputStream.read(buffer))!=-1){
                fileOutputStream.write(buffer,0,len);
            }

//            fileOutputStream.close();
//            inputStream.close();


            System.out.println("输出完成---");
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("Ok...".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if(serverSocket!=null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    if(!socket.isClosed())
                       socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void client(){

        Socket socket = null;
        OutputStream outputStream = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"),8899);
            outputStream = socket.getOutputStream();
            File file = new File("E:\\JavaProjects\\LearnProjects\\learn-practice\\java-base\\src\\main\\java\\com\\lyf\\language\\learn\\atsgg\\network\\hello.txt");
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len;
            while ((len=fileInputStream.read(buffer))!=-1){
                outputStream.write(buffer,0,len);
            }
            System.out.println("Client发生成功..");

            fileInputStream.close();
            //outputStream.close();
            // 只能关闭输出、输入的一个方向流，而不能直接关闭流，否则会关闭socket>>>?
            socket.shutdownOutput();


            // 获取--Input--之前未关闭Out..产生死锁？
            InputStream inputStream= socket.getInputStream();
            while ((len=inputStream.read(buffer))!=-1){//从IO流读到buffer；write，从buffer写到流
                System.out.println("正在读取..."+len);
                System.out.println(new String(buffer,0,len));
            }
            inputStream.close();
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(socket!=null) {
                try {
                    if(!socket.isClosed())
                        socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outputStream!=null) {
                try {

                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
